package cn.hyq.org.controller;


import cn.hyq.basic.util.AjaxResult;
import cn.hyq.basic.util.PageList;
import cn.hyq.org.domain.Department;
import cn.hyq.org.query.DepartmentQuery;
import cn.hyq.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/dept")
//接口文档的注解
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;


    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Department> queryAll(){
        System.out.println("执行了controller的查询所有方法");
        return departmentService.queryAll();
    }

    /**
     * 高级查询加分页
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        System.out.println("执行了controller的高级查询加分页");
        return departmentService.queryPage(query);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("你搞错了");
        }
    }

    /**
     * 新增或修改
     * @param department
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if (department.getId()!=null){
                departmentService.update(department);
            }else {
                departmentService.save(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("你搞错了");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            System.out.println(ids);
            departmentService.batchRemoveById(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("你搞错了");
        }
    }

    /**
     * 无限极树查询
     * @return
     */
    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }
}
